<template>
  <div class="login">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="ruleForm"
    >
      <el-form-item>
        <div class="avatar">
          <img src="../../assets/logo.png" alt="" />
        </div>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
          class="loginButton"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')" class="resetButton"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getLogin } from "../../api/index"

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (!value) return callback(new Error("密码不能为空"));

      if (!value.includes("1")) {
        callback(new Error("密码必须包含1"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 18,
            message: "长度在 3 到 18 个字符",
            trigger: "blur",
          },
          { validator: validatePass, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 发送请求
          // console.log(this.ruleForm);
          getLogin(this.ruleForm).then( res => {
            let { meta: { status, msg } } = res;
              if(status == 200) {
                let {data: { token, username}} = res
                // 使用elementUI中的消息提示提示登陆成功
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
  
                this.$store.commit('setToken', token);
                this.$store.commit('setUsername', username)
  
                this.$router.push({ path: "/home" })
              }else {
                // console.log(res.meta.msg);+
                this.$message.error( msg );
              }
            
          } )
          // alert("submit!");
        } else {
          console.log("提交失败");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang="less">
.ruleForm {
  box-sizing: border-box;
  width: 450px;
  height: 300px;
  margin: 200px auto;
  background-color: #fff;
  border-radius: 5px;
  padding: 50px 40px 10px 10px;
  box-shadow: 0 1px 5px #ccc;
}
.ruleForm .loginButton {
  margin-left: 120px;
  margin-right: 10px;
  width: 80px;
}
.ruleForm .resetButton {
  width: 80px;
}
.avatar {
      position: relative;
      left: 40%;
      top: -50px;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
      background-color: #fff;
      text-align: center;
      vertical-align: middle;
      img {
        width: 90px;
        height: 90px;
        margin-top: 20px;
      }
    }
</style>
